package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcx.ldzj.dto.RoleInfoDTO;
import com.jcx.ldzj.emun.MyCode;
import com.jcx.ldzj.emun.SystemCode;
import com.jcx.ldzj.entity.RoleInfo;
import com.jcx.ldzj.entity.UserRoleRelation;
import com.jcx.ldzj.service.IRoleInfoService;
import com.jcx.ldzj.service.IUserRoleRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ld
 * @since 2021-07-19
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/role-info")
@Api(value = "RoleInfoController", tags = "RoleInfoController")
public class RoleInfoController extends ApiController {
        @Autowired
        private IRoleInfoService iRoleInfoService;
        @Autowired
        private IUserRoleRelationService iUserRoleRelationService;
       /**
       * 列表
       * @param pageNum
       * @param pageSize
       * @param roleInfo
       * @return
       */
       @GetMapping("/page")
       @RequiresPermissions("basic:role:page")
       @ApiOperation(value = "列表", notes = "传入RoleInfo对象")
       public R<IPage<RoleInfo>> page(Integer pageNum, Integer pageSize,RoleInfo roleInfo) {
            QueryWrapper<RoleInfo> queryWrapper=new QueryWrapper<>(roleInfo);
           queryWrapper.orderByDesc("update_time_");
           if(roleInfo.getRoleName()!=null){
               queryWrapper.like("role_name_",roleInfo.getRoleName());
               roleInfo.setRoleName(null);
           }
            IPage<RoleInfo> page = iRoleInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
            return R.ok(page);
       }
       /**
       * 详情
       * @param id
       * @return
       */
       @GetMapping("/{id}")
       @ApiOperation(value = "详情", notes = "传入id")
       @RequiresPermissions("basic:role:view")
       public R<RoleInfoDTO> view(@PathVariable("id") String id) {
           RoleInfoDTO user = iRoleInfoService.getRoleById(id);
          return R.ok(user);
        }

        /**
        * 新增
        * @param roleInfo
        * @return
        */
        @PostMapping
        @ApiOperation(value = "新增", notes = "传入RoleInfo")
        @RequiresPermissions("basic:role:add")
        public R add(@Valid @RequestBody RoleInfoDTO roleInfo) {

          return iRoleInfoService.addRole(roleInfo);
        }


        /**
        * 修改角色
        * @param roleInfo
        * @return
        */
        @PutMapping
        @RequiresPermissions("basic:role:update")
        @ApiOperation(value = "修改", notes = "传入RoleInfo")
        public R edit(@Valid @RequestBody RoleInfoDTO roleInfo) {
          return iRoleInfoService.updateRole(roleInfo);
        }

        /**
        * 删除
        */
        @DeleteMapping("/{id}")
        @RequiresPermissions("basic:role:delete")
        @ApiOperation(value = "删除", notes = "传入ids")
        public R del(@ApiParam(value = "主键集合", required = true) @PathVariable("id") String id) {
            List<UserRoleRelation> lsit = iUserRoleRelationService.getByroleId(id);
            if(lsit==null||lsit.size()==0){
                if(SystemCode.IS_ADMIN.equals(iRoleInfoService.getById(id).getRoleCode())){
                    return this.failed(MyCode.该角色标识为超级管理员_不能删除);
                }
                return this.success(iRoleInfoService.removeById(id));
            }
            return this.failed(MyCode.该角色已经分配了用户_不允许删除_请先取消该角色对应的用户);
        }

        /**
         * 列表，用于前端下拉框操作
         * @return
         */
        @GetMapping("/list")
        @ApiOperation(value = "列表", notes = "传入RoleInfo对象")
        public R<List<RoleInfo>> list() {
            RoleInfo roleInfo=new RoleInfo();
            roleInfo.setState('1');
            QueryWrapper<RoleInfo> queryWrapper=new QueryWrapper<>(roleInfo);
            List<RoleInfo> list = iRoleInfoService.list(queryWrapper);
            return R.ok(list);
        }
 }
